﻿// <copyright file="ModemAcknowledgeTelegram.cs" company="Hallvard Korsgaard">
//  Microsoft Public License (Ms-PL). See license details here: http://enoceannet.codeplex.com/license
// </copyright>
// <author>Hallvard Korsgaard</author> 

namespace EnOcean.Radio.Modem
{
  using System;

  /// <summary>
  /// Represents a modem acknowledge telegram (ORG = 0x0B).
  /// </summary>
  [Serializable]
  public class ModemAcknowledgeTelegram : RadioTelegram
  {
     /// <summary>
    /// Variable for the AnsweringId property.
    /// </summary>
    private int answeringId;

    /// <summary>
    ///  Initializes a new instance of the ModemAcknowledgeTelegram class.
    /// </summary>
    public ModemAcknowledgeTelegram()
    {
      this.TelegramType = TelegramType.TransmitRadioTelegram;
    }

    /// <summary>
    /// Gets or sets the 16 bit modem ID of the answering modem. The ID must be in the range 0 to 0xffff inclusive.
    /// </summary>
    public int AnsweringId
    {
      get
      {
        return this.answeringId;
      }

      set
      {
        if (value >= 0x0 && value <= 0xffff)
        {
          this.answeringId = value;
        }
        else
        {
          throw new ArgumentOutOfRangeException("AnsweringId", "must be a 16 bit value in the range 0x0 to 0xffff inclusive");
        }
      }
    }

    /// <summary>
    /// Creates the byte format of the message.
    /// </summary>
    /// <param name="message">A byte array of length 14 to hold the message contents.</param>
    protected internal override void CreateMessage(byte[] message)
    {
      base.CreateMessage(message);
      message[3] = (byte)Org.ModemAcknowledge; // Org
      message[8] = (byte)(this.answeringId >> 8); // Modem Id 1
      message[9] = (byte)(this.answeringId & 0xff); // Modem Id 0
      message[12] = this.RepeatCounter; // Status
    }
  }
}
